<?php
/*
  $Id: checkout_shipping.php
*/

  if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );


  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {

      tep_db_connect() or die('Unable to connect to database server!');
    }
    // Load osCommerce Globals for Boxes
    global $category_links, $languages_id, $catID, $customer_id, $catID_array, $current_category_id;
    global $request_type, $lng, $cart, $currencies, $language, $navigation, $breadcrumb, 
    $shipping,$order, $total_count, $shipping_modules;
  } else {
    // Load osCommerce Environment for the First Time
    require('components/'.$option.'/includes/application_top.php');
  }
if (!tep_session_is_registered('shipping')) tep_session_register('shipping');
  require('includes/classes/http_client.php');

  if (!tep_session_is_registered('customer_id') && $ecjc_pwa != 1) {
    $navigation->set_snapshot();
    if ( $mosUser['validated'] ) {
		tep_redirect(tep_href_link(FILENAME_CREATE_ACCOUNT, 'backto=checkout_shipping', 'SSL'));
    } else {
		   tep_redirect(tep_href_link(FILENAME_LOGIN, '', 'SSL'));
        
      
    }
  }
  
  
// if checkout is disabled, show disabled page.
   if (DISABLE_CHECKOUT == 'true') {
	tep_redirect(tep_href_link(FILENAME_CHECKOUT_DISABLE));
   }
// if there is nothing in the customers cart, redirect them to the shopping cart page
  if ($cart->count_contents() < 1) {
    tep_redirect(tep_href_link(FILENAME_SHOPPING_CART));
  }

// if no shipping destination address was selected, use the customers own address as default
  if (!tep_session_is_registered('sendto')) {
    tep_session_register('sendto');
    $sendto = $customer_default_address_id;
  } else {
   if ($ecjc_pwa == 1) {
      $sendto = 1;
      }else{

   
   if ( (is_array($sendto) && empty($sendto)) || is_numeric($sendto) ) { 
         $check_address_query = tep_db_query("select count(*) as total from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . (int)$customer_id . "' and address_book_id = '" . (int)$sendto . "'"); 
       $check_address = tep_db_fetch_array($check_address_query); 


    if ($check_address['total'] != '1') {
      $sendto = $customer_default_address_id;
      if (tep_session_is_registered('shipping')) tep_session_unregister('shipping');
    }
    }
  }
  }

  require(DIR_WS_CLASSES . 'order.php');
  $order = new order;

// register a random ID in the session to check throughout the checkout procedure
// against alterations in the shopping cart contents
  if (!tep_session_is_registered('cartID')) tep_session_register('cartID');
  $cartID = $cart->cartID;


if (SELECT_VENDOR_SHIPPING != 'true') {
  if (($order->content_type == 'virtual') || ($order->content_type == 'virtual_weight') ) { // Edited for CCGV
    if (!tep_session_is_registered('shipping')) tep_session_register('shipping');
    $shipping = false;
    $sendto = false;
    tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
  }
  }

//MVS
  if (SELECT_VENDOR_SHIPPING == 'true') {
    include(DIR_WS_CLASSES . 'vendor_shipping.php');
    $shipping_modules = new shipping;
  } else {
    include(DIR_WS_CLASSES . 'shipping.php');
    $shipping_modules = new shipping;
    $total_weight = $cart->show_weight();
    $cost = $cart->show_total();
    $total_count = $cart->count_contents();
  }
  

  if ( isset($_POST['action']) && ($_POST['action'] == 'process') ) {
    if (!tep_session_is_registered('comments')) tep_session_register('comments');
    if (tep_not_null($_POST['comments'])) {
      $comments = tep_db_prepare_input($_POST['comments']);
    }

    if (!tep_session_is_registered('shipping')) tep_session_register('shipping');

   if (SELECT_VENDOR_SHIPPING == 'true') {

      $total_shipping_cost = 0;
      $shipping_title = MULTIPLE_SHIP_METHODS_TITLE;
      $vendor_shipping = $cart->vendor_shipping;
      $shipping = array();

      foreach ($vendor_shipping as $vendor_id => $vendor_data) {

        $products_shipped = $_POST['products_' . $vendor_id];
        $products_array = explode ("_", $products_shipped);

        $shipping_data = $_POST['shipping_' . $vendor_id];

        $shipping_array = explode ("_", $shipping_data);

        $module = $shipping_array[0];
        $method = $shipping_array[1];
        $ship_tax = $shipping_array[2];
global $$module;
        if ( is_object($$module) || ($module == 'free') ) {
          if ($module == 'free') {
            $quote[0]['methods'][0]['title'] = FREE_SHIPPING_TITLE;
            $quote[0]['methods'][0]['cost'] = '0';
          } else {
            $total_weight = $vendor_shipping[$vendor_id]['weight'];
            $shipping_weight = $total_weight;
            $cost = $vendor_shipping[$vendor_id]['cost'];
            $total_count = $vendor_shipping[$vendor_id]['qty'];
            $quote = $shipping_modules->quote($method, $module, $vendor_id);

          }
          if (isset($quote['error'])) {
            tep_session_unregister('shipping');
          } else {
            if ( (isset($quote[0]['methods'][0]['title'])) && (isset($quote[0]['methods'][0]['cost'])) ) {
              $output[$vendor_id] = array('id' => $module . '_' . $method,
                                          'title' => $quote[0]['methods'][0]['title'],
                                          'ship_tax' => $ship_tax,
                                          'products' => $products_array,
                                          'cost' => $quote[0]['methods'][0]['cost']
                                         );
              $total_ship_tax += $ship_tax;
              $total_shipping_cost += $quote[0]['methods'][0]['cost'];
            }//if isset
          }//if isset
        }//if is_object
      }//foreach

      if ($free_shipping == true) {
        $shipping_title = $quote[0]['module'];
      } elseif (count($output) <2) {
        $shipping_title = $quote[0]['methods'][0]['title'];
        }
     $shipping = array('id' => $shipping,
                        'title' => $shipping_title,
                        'cost' => $total_shipping_cost,
                        'shipping_tax_total' => $total_ship_tax,
                        'vendor' => $output
                       );

    } else {
      if ( (isset($_POST['shipping'])) && (strpos($_POST['shipping'], '_')) ) {
        $shipping = $_POST['shipping'];
       
        list($module, $method) = explode('_', $shipping);
        global $$module;
        
        
        if ( is_object($$module) || ($shipping == 'free_free') ) {
        
          if ($shipping == 'free_free') {
            $quote[0]['methods'][0]['title'] = FREE_SHIPPING_TITLE;
            $quote[0]['methods'][0]['cost'] = '0';
          } else {
            $quote = $shipping_modules->quote($method, $module);
            
          }
        }

        if (isset($quote['error'])) {
          tep_session_unregister('shipping');
        } else {
          if ( (isset($quote[0]['methods'][0]['title'])) && (isset($quote[0]['methods'][0]['cost'])) ) {
            $shipping = array('id' => $shipping,
                              'title' => (($free_shipping == true) ?  $quote[0]['methods'][0]['title'] : $quote[0]['module'] . ' (' . $quote[0]['methods'][0]['title'] . ')'),
                              'cost' => $quote[0]['methods'][0]['cost']);

            tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
          }
        }
      } else {
        tep_session_unregister('shipping');
      }
    }
    tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, '', 'SSL'));
    exit;
  }  //MVS END



// get all available shipping quotes
  $quotes = $shipping_modules->quote();

  if ( !tep_session_is_registered('shipping') || ( tep_session_is_registered('shipping') && ($shipping == false) && (tep_count_shipping_modules() > 1) ) ) $shipping = $shipping_modules->cheapest();


  require(DIR_WS_LANGUAGES . $language . '/' . $osMod . '.php');

  $breadcrumb->add(NAVBAR_TITLE_1, tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
  $breadcrumb->add(NAVBAR_TITLE_2, tep_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'));
?>

<script language="javascript"><!--
var selected=null;

function selectRowEffect(object, buttonSelect) {
  if (!selected) {
    if (document.getElementById) {
      selected = document.getElementById('defaultSelected');
    } else {
      selected = document.all['defaultSelected'];
    }
  }

  if (selected) selected.className = 'moduleRow';
  object.className = 'moduleRowSelected';
  selected = object;

// one button is not an array
  if (document.checkout_address.shipping[0]) {
    document.checkout_address.shipping[buttonSelect].checked=true;
  } else {
    document.checkout_address.shipping.checked=true;
  }
}

function rowOverEffect(object) {
  if (object.className == 'moduleRow') object.className = 'moduleRowOver';
}

function rowOutEffect(object) {
  if (object.className == 'moduleRowOver') object.className = 'moduleRow';
}
//--></script>


<?php require(DIR_WS_INCLUDES . 'header.php'); 
require(WS_TEMPLATES. E_TEMPLATE_FOLDER . '/'.$osMod.'.html');
require(DIR_WS_INCLUDES . 'footer.php');
require(DIR_WS_INCLUDES . 'application_bottom.php');

  if (!DB_SHARED) {
    global $database, $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix;
    $database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
  }
 ?>
